<?php
header('Content-Type:text/html;charset=utf-8');
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "LOVE";
 
// 创建连接
$conn = mysqli_connect($servername, $username, $password,$dbname);
 
// 检测连接
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}
echo "连接成功","<br/>";

// 创建数据库
/*
$sql = "CREATE DATABASE LOVE";
if (mysqli_query($conn, $sql)) {
    echo "数据库创建成功";
} else {
    echo "Error creating database: " . mysqli_error($conn);
}
*/

// 使用 sql 创建数据表
/*
$sql = "CREATE TABLE WOMEN (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, 
firstname VARCHAR(30) NOT NULL,
lastname VARCHAR(30) NOT NULL,
levels VARCHAR(50),
reg_date TIMESTAMP
)";
     
if (mysqli_query($conn, $sql)) {
    echo "数据表 WOMEN 创建成功";
} else {
    echo "创建数据表错误: " . mysqli_error($conn);
}
*/
//插入数据
/*
$sql = "INSERT INTO WOMEN (firstname, lastname, levels)
VALUES ('John', 'Doe', 'low');";
$sql .= "INSERT INTO WOMEN (firstname, lastname, levels)
VALUES ('Mary', 'Moe', 'high');";
$sql .= "INSERT INTO WOMEN (firstname, lastname, levels)
VALUES ('Julie', 'Dooley', 'middle')";
 
if (mysqli_multi_query($conn, $sql)) {
    echo "新记录插入成功";
} else {
    echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
*/
//读取数据
/*
$sql = "SELECT id, firstname, lastname levels FROM WOMEN";
$result = mysqli_query($conn, $sql);
 
if (mysqli_num_rows($result) > 0) {
    // 输出数据
    while($row = mysqli_fetch_assoc($result)) {
        echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["levels"]."<br>";
        //echo $row['firstname'] . " " . $row['lastname'] . " " . $row['levels'];
    }
} else {
    echo "0 结果";
}
//为什么lastname无法显示出来
*/

//where字句查询
/*
$result = mysqli_query($conn,"SELECT * FROM WOMEN
WHERE firstname='Mary'");

while($row = mysqli_fetch_array($result))
{
    echo $row['id'] . $row['firstname'] . " " . $row['lastname'] . " " . $row['levels'];
    echo "<br>";
}
*/

//排序输出
/*
$result = mysqli_query($conn,"SELECT * FROM WOMEN ORDER BY levels");

while($row = mysqli_fetch_array($result))
{
    echo $row['firstname'];
    echo " " . $row['lastname'];
    echo " " . $row['levels'];
    echo "<br>";
}
*/

//更新
/*
mysqli_query($conn,"UPDATE WOMEN SET levels='high'
WHERE firstname='John'");
*/

//删除数据
/*
mysqli_query($conn,"DELETE FROM WOMEN WHERE lastname='Doe'");
*/
mysqli_close($conn);

?>
